<?php

namespace app\admin\model;

use think\Model;


class DiyPushModel extends Model
{

    

    

    // 表名
    protected $name = 'diy_push';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text'
    ];


    public function user()
    {
        return $this->hasOne('app\admin\model\User', 'id', 'user_id');
    }
    public function house()
    {
        return $this->hasOne('app\admin\model\HouseModel', 'id', 'house_id');
    }

    public function getStatusList()
    {
        return ['0' =>'待推送', '1' => '推送成功', 2 => '推送失败'];
    }


    public function getHouseTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['house_type'] ?? '');
        $list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_HOUSE);
        return $list[$value] ?? '';
    }

    public function getPaymentTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['payment_type'] ?? '');
        $list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_PAYMENT);
        return $list[$value] ?? '';
    }

    public function getRentTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['rent_type'] ?? '');
        $list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_RENT);
        return $list[$value] ?? '';
    }

    public function getCommunityTextAttr($value, $data)
    {
        $value = $value ?: ($data['community'] ?? '');
        $list = ConfigValueModel::getCommunityList();
        return $list[$value] ?? '';
    }

    public function getTagsTextAttr($value, $data)
    {
        $tags = $value ?: ($data['tags'] ?? '');
        return ConfigValueModel::getTagsText($tags);
    }
    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
